﻿using System;
using System.ServiceModel;
using System.Windows.Controls;
using System.Windows.Navigation;
using PmuRegistry.Silverlight.ModalDialogs;
using PmuRegistry.Silverlight.PmuRegistryDataServiceProxy;
using PmuRegistry.Silverlight.Utilities;
using System.Windows.Data;
using System.Windows;

namespace PmuRegistry.Silverlight.Pages
{
	public partial class Comments : Page
	{
		PhasorDataServiceClient m_client;

		public Comments()
		{
			InitializeComponent();
			m_client = Common.GetPhasorDataServiceClient();
			m_client.GetUserCommentListCompleted += new EventHandler<GetUserCommentListCompletedEventArgs>(m_client_GetUserCommentListCompleted);
			m_client.SaveUserCommentCompleted += new EventHandler<SaveUserCommentCompletedEventArgs>(m_client_SaveUserCommentCompleted);
			ButtonClear.Click += new System.Windows.RoutedEventHandler(ButtonClear_Click);
			ButtonSave.Click += new System.Windows.RoutedEventHandler(ButtonSave_Click);
		}

		void m_client_SaveUserCommentCompleted(object sender, SaveUserCommentCompletedEventArgs e)
		{
			SystemMessages sm;
			if (e.Error == null)
			{
				ClearForm();
				sm = new SystemMessages(new Message() { UserMessage = e.Result, SystemMessage = string.Empty, UserMessageType = MessageType.Success },
						ButtonType.OkOnly);
			}
			else
			{
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Save User Comment Information", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
			}
			sm.Show();
			m_client.GetUserCommentListAsync();
		}

		void ButtonSave_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			UserComment comment = new UserComment();
			comment.UserName = ((App)Application.Current).UserName;
			comment.Subject = TextBoxSubject.Text;
			comment.Comment = TextBoxComment.Text;
			m_client.SaveUserCommentAsync(comment);
		}

		void ButtonClear_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			ClearForm();
		}

		void m_client_GetUserCommentListCompleted(object sender, GetUserCommentListCompletedEventArgs e)
		{
			if (e.Error == null)
			{
				PagedCollectionView commentsList = new PagedCollectionView(e.Result);
				ListBoxCommentList.ItemsSource = commentsList;
				DataPagerComments.Source = commentsList;
			}
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve User Comments List", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
		}

		// Executes when the user navigates to this page.
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			m_client.GetUserCommentListAsync();
		}

		void ClearForm()
		{
			TextBoxSubject.Text = string.Empty;
			TextBoxComment.Text = string.Empty;
		}

	}
}
